package com.zhongge.controller;

import com.zhongge.entity.PageData;
import com.zhongge.entity.Student;
import com.zhongge.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    //调用业务逻辑层的对象
    @Autowired
    private StudentService studentService;

    //注入资源加载器
    @Autowired
    private ResourceLoader resourceLoader;

    /**
     * 分页查询
     * @param pageNum 页码
     * @param pageSize 页数
     * @return 分页信息
     */
    @GetMapping("/pageSelect.do")
    @ResponseBody
    public PageData pageSelect(@RequestParam("page") Integer pageNum,
                               @RequestParam("rows") Integer pageSize){
        return studentService.pageSelect(pageNum, pageSize);
    }

    /**
     * 根据学生id查询学生信息
     * @param id 学生编号
     * @return 返回一个学生信息
     */
    @GetMapping("/selectStudentById.do")
    @ResponseBody
    public Student selectStudentById(Integer id){
        return studentService.selectStudentById(id);
    }


    /**
     * 添加学生
     * @param student 学生
     * @return 添加信息
     */
    @PostMapping("/addStudent.do")
    @ResponseBody
    public Map<String, Object> addStudent(Student student){
        return studentService.addStudent(student);
    }

    /**
     * 根据id修改学生信息
     * 注意：为了其他字段可以使用此处必须参数是student
     * @param student 学生
     * @return 修改信息
     */
    @PostMapping("/updateStudent.do")
    @ResponseBody
    public Map<String, Object> updateStudent(Student student){
        Map<String, Object> map = new HashMap<>();
        try{
            map = studentService.updateStudent(student);
        }catch (DuplicateKeyException duplicateKeyException){
            map.put("message","学号已存在！请更换学号后重试~~");
            map.put("success",false);
        }catch (Exception e){
            map.put("message","系统异常，请稍后重试~~");
            map.put("success",false);
        }
        return map;
    }



    /**
     * 根据学生编号修改学生信息
     * @param id 学生编号
     * @return 删除信息
     */
    @PostMapping("/deleteStudent.do")
    @ResponseBody
    public Map<String, Object> deleteStudent(Integer id){
        return studentService.deleteStudent(id);
    }


    @GetMapping("/index")
    public String index(){
        return "stuInfoSys/index";
    }

    @GetMapping("/dispatchStu")
    public String dispatchStu(){
        return "stuInfoSys/student/list";
    }
    @GetMapping("/dispatchCourse")
    public String dispatchCourse(){
        return "stuInfoSys/course/main";
    }
    @GetMapping("/dispatchScore")
    public String dispatchScore(){
        return "stuInfoSys/score/main";
    }

    @GetMapping("/addPage")
    public String addPage(){
        return "stuInfoSys/student/add";
    }

    @GetMapping("/updatePage/{id}")
    public ModelAndView updatePage(@PathVariable("id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Student student = studentService.selectStudentById(id);

        modelAndView.setViewName("stuInfoSys/student/update");
        modelAndView.addObject("student",student);
        return modelAndView;
    }

    //实现后端响应JSON文件
    @PostMapping("/getResource")
    @ResponseBody
    public String getResource(){
        //classpath:/templates/stuInfoSys/json/menu.json
        //首先通过或资源加载器获取资源对象
        Resource resource = resourceLoader.getResource("classpath:/templates/stuInfoSys/json/menu.json");


        File file = null;
        FileInputStream fileInputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        try{
            file = resource.getFile();
            fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int read = fileInputStream.read(bytes);
            while (read != -1){
                String s = new String(bytes, 0, read);
                stringBuilder.append(s);
                read = fileInputStream.read(bytes);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return stringBuilder.toString();
    }
}
